﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Globalization;
using System.ComponentModel;

namespace Gmantis.Controls
{
    public class ColorConverter : IValueConverter
    {
        internal static object Convert(object value, Type targetType)
        {
            Color white = Colors.White;
            if (value is SolidColorBrush)
            {
                white = ((SolidColorBrush)value).Color;
            }
            else if (value is GradientBrush)
            {
                GradientBrush brush = (GradientBrush)value;
                if (brush.GradientStops.Count > 0)
                {
                    white = brush.GradientStops[0].Color;
                }
            }
            else if (value is Color)
            {
                white = (Color)value;
            }
            else if (value is double)
            {
                double num = (double)value;
                white = new HslColor((float)num, 1f, 0.5f).ToColor();
            }
            if (targetType.Name == "Color")
            {
                return white;
            }
            if (targetType.Name == "Brush")
            {
                return new SolidColorBrush(white);
            }
            return value;
        }

        object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Convert(value, targetType);
        }

        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Convert(value, targetType);
        }
    }

    public class ZoomUnitTypeConverter : TypeConverter
    {
        // Methods
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return (sourceType.FullName == "System.String");
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            double num;
            string s = value as string;
            if (s == null)
            {
                return null;
            }
            s = s.Trim();
            switch (s)
            {
                case "Fill":
                    return ZoomUnit.Fill;

                case "OneItem":
                    return ZoomUnit.OneItem;
            }
            ZoomUnitType @fixed = ZoomUnitType.Fixed;
            if (!double.TryParse(s, NumberStyles.Any, (IFormatProvider)CultureInfo.InvariantCulture.NumberFormat, out num))
            {
                num = 1.0;
            }
            return new ZoomUnit(num, @fixed);
        }
    }
}
